package com.shuqing.controller;

import com.shuqing.domain.Record;
import com.shuqing.domain.User;
import com.shuqing.service.RecordService;
import com.shuqing.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: ShuqingLibrary
 * @ClassName RecordController
 * @description:
 * @author: Ats
 * @create: 2025-04-07 16:31
 * @Version 1.0
 */
@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @RequestMapping("/searchRecords")
    public ModelAndView searchRecords(Record record, HttpServletRequest request,
                                      Integer pageNum, Integer pageSize){
//如果前端没有设置页面，那么默认第一页
        if (pageNum == null) {
            pageNum = 1;
        }
        //如果前端没有设置页面显示条数，那么默认每页显示10条数据
        if (pageSize == null) {
            pageSize = 10;
        }
        //获取当前用户
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        PageResult pageResult = recordService.searchRecords(record, user, pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("record");
        mv.addObject("pageResult", pageResult);
        //回显查询输入框的数据
        mv.addObject("search", record);
        //显示页码
        mv.addObject("pageNum", pageNum);
        //当前页面，改变页码时，路径不变
        mv.addObject("gourl",request.getRequestURI());
        return mv;
    }

}
